package org.example.test_rocketmq;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MessageSender {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Scheduled(fixedRate = 5000)
    public void run() {
        String topic = "testTopic001";
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = f.format(new Date());
        String message = "这是一条消息，发送时间：" + time + "。";
        System.out.println("发送消息：" + message);
        rocketMQTemplate.syncSend(topic, message);
    }

}
